if(!coos){var coos = function(){};}
Number.prototype.NaN0 = function()
{
	return isNaN(this)?0:this;
};
var selectedIndex = -1;
var intTmp;
coos.searchMenu = function(){};
coos.searchMenu.onkeyup = function(obj,showDiv,treeDiv,searchButton,event)
{
	var obj = obj;
	var showDiv = coos.$obj(showDiv);
	var treeDiv = coos.$obj(treeDiv);
	var e = coos.event.format(event);
	var keyCode = e.keyCode;
	var isUp = false;
	showDiv.onmouseover = function(){coos.searchMenu.timeout = null;};
	if(keyCode==40 || keyCode==38)
	{
		if(keyCode==40){isUp = true;}
		coos.searchMenu.chageSelection(obj,showDiv,treeDiv,searchButton,e,isUp);
	}
	else if(keyCode == 13)
	{
		coos.searchMenu.outSelection(obj,showDiv,treeDiv,searchButton,e,selectedIndex);
	}
	else
	{
		coos.searchMenu.show(obj,showDiv,treeDiv,searchButton,e);
	}
};
coos.searchMenu.timeout = null;
coos.searchMenu.onblur = function(showDiv)
{
	var showDiv = coos.$obj(showDiv);
	coos.searchMenu.timeout = setTimeout(function(){coos.searchMenu.close(showDiv);},300);
};
coos.searchMenu.show = function(obj,showDiv,treeDiv,searchButton,event)
{
	var obj = obj;
	var showDiv = coos.$obj(showDiv);
	var treeDiv = coos.$obj(treeDiv);
	var e = coos.event.format(event);
	var value = obj.value;
	if(value.trim() == "")
	{
		return;
	}
	selectedIndex = -1;
	showDiv.innerHTML = "";
	var key = obj.value.trim();
	var re = "(<a([^>])+>([^<])*" + key + "([^<])*</a>)";
	var oReg = new RegExp(re, "ig");
	var arr = treeDiv.innerHTML.match(oReg);
	if(arr !== null)
	{
		var len = arr.length;
		for(var i = 0; i < len; i++)
		{
			coos.searchMenu.addOption(obj,showDiv,arr[i],searchButton);
		}
	}
	showDiv.innerHTML += "<div class=close onclick=coos.searchMenu.close(\"" + showDiv.id + "\")>关闭&nbsp;</div>";
	coos.searchMenu.divPosition(obj,showDiv);
	showDiv.style.display = "";
};
coos.searchMenu.chageSelection = function(obj,showDiv,treeDiv,searchButton,event,isUp)
{
	if(isUp)
	{
		selectedIndex++;
	}
	else
	{
		selectedIndex--;
	}
	var divs = coos.$tag("div",showDiv);
	var maxIndex = divs.length - 2;
	if(selectedIndex < 0){selectedIndex = 0;}
	if(selectedIndex > maxIndex){selectedIndex = maxIndex;}
	for(intTmp = 0; intTmp <= maxIndex; intTmp++)
	{
		if(intTmp == selectedIndex)
		{
			divs[intTmp].className = "selected";
			obj.value = coos.$tag("a",divs[intTmp])[0].innerHTML;
		}
		else
		{
			divs[intTmp].className = "";
		}
	}
};
coos.searchMenu.outSelection = function(obj,showDiv,treeDiv,searchButton,event,Index)
{
	var divs = coos.$tag("div",showDiv);
	var maxIndex = divs.length - 2;
	if(maxIndex >= Index)
	{
		obj.value = coos.$tag("a",divs[Index])[0].innerHTML;
	}
	showDiv.innerHTML = "";
	showDiv.style.display = "none";
};
coos.searchMenu.close = function(showDiv)
{
	var showDiv = coos.$obj(showDiv);
	showDiv.innerHTML = "";
	showDiv.style.display = "none";
};
coos.searchMenu.divPosition = function(obj,showDiv)
{
	var obj = obj;
	var inputObj = obj;
	var showDiv = showDiv;
	if(coos.browser.msie)
	{
		var top = 0;
		var left = -2;
	}
	else
	{
		var top = 2;
		var left = 0;
	}
	while(obj.offsetParent)
	{
		left += obj.offsetLeft + (obj.currentStyle?(parseInt(obj.currentStyle.borderLeftWidth)).NaN0():0);
		top += obj.offsetTop + (obj.currentStyle?(parseInt(obj.currentStyle.borderTopWidth)).NaN0():0);
		obj = obj.offsetParent;
	}
	left += obj.offsetLeft + (obj.currentStyle?(parseInt(obj.currentStyle.borderLeftWidth)).NaN0():0);
	top += obj.offsetTop + (obj.currentStyle?(parseInt(obj.currentStyle.borderTopWidth)).NaN0():0);
	showDiv.style.top = (top + inputObj.clientHeight) + "px";
	showDiv.style.left = left + "px";
	showDiv.style.width = inputObj.clientWidth + 1 + "px";
};
coos.searchMenu.addOption = function(obj,showDiv,a,searchButton)
{
	var re = ">(.+)<";
	var oReg = new RegExp(re, "ig");
	var arr = a.match(oReg);
	var text = arr[0].substring(1,arr[0].length-1);
	var re = "href=\"javascript:([^\"])+";
	var oReg = new RegExp(re, "ig");
	arr = a.match(oReg);
	var script = "";
	if(arr)
	{
		script = arr[0].substring(17,arr[0].length);
	}
	var newa = a.replace(/id="([^"]*)"/,"id=\"sm_$1\"");
	showDiv.innerHTML +="<div onmouseover=this.className=\"selected\";document.getElementById(\""+obj.id+"\").value=\"" + text + "\";document.getElementById(\""+searchButton+"\").onclick=function(){" + script + "} onclick=" + script + " onmouseout='this.className=\"\"' onmousedown='document.getElementById(\""+obj.id+"\").value=\"" + text + "\"'>" + newa + "</div>";
};
